package com.middlegroup.subjectpay.web.api;

import com.middlegroup.common.enums.ServiceType;
import com.middlegroup.common.model.ResponseEntity;
import com.middlegroup.common.model.TransResult;
import com.middlegroup.subjectpay.dto.BindCardDto;
import com.middlegroup.subjectpay.dto.TransRequest;
import com.middlegroup.subjectpay.service.BindCardService;
import com.middlegroup.subjectpay.service.ProtocolPayService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 *
 * @author pc
 * @date 2019/1/15
 */
@RestController
public class ProtocolPayController extends BaseController {
    @Resource
    private BindCardService bindCardService;
    @Resource
    private ProtocolPayService protocolPayService;

    /**通用协议支付绑卡接口(包括绑卡申请，绑卡确认，重发短信验证码)
     curl -i 'http://10.0.0.109:8089/bindCard' -d 'serviceType=BINDCARDAPPLY&name=成娓娓&cardPhone=18321788695&cardNo=6217001210084599560&idCard=421302199202081224&subjectCode=S000001&channelCode=CH3333331&merchantBillNo=merchantBillNo201901270001&billNo=billNo201901270001'
     curl -i 'http://10.0.0.109:8089/bindCard' -d 'serviceType=BINDCARDCONFIRM&smsCode=111111&subjectCode=S000001&merchantBillNo=merchantBillNo201901270001&billNo=billNo201901270001'
     curl -i 'http://10.0.0.109:8089/bindCard' -d 'serviceType=RESENDSMS&subjectCode=S000001&merchantBillNo=merchantBillNo201901270001&billNo=billNo201901270001'

     curl -i 'http://10.0.0.109:8089/bindCard' -d 'serviceType=BINDCARDAPPLY&name=成娓娓&cardPhone=18321788695&cardNo=6217001210084599560&idCard=421302199202081224&subjectCode=S000001&channelCode=EMPTY0000003&merchantBillNo=merchantBillNo201901270014&billNo=billNo201901270014'
     curl -i 'http://10.0.0.109:8089/bindCard' -d 'serviceType=BINDCARDCONFIRM&smsCode=775463&subjectCode=S000001&merchantBillNo=merchantBillNo201901270014&billNo=billNo201901270014'
     curl -i 'http://10.0.0.109:8089/bindCard' -d 'serviceType=RESENDSMS&subjectCode=S000001&merchantBillNo=merchantBillNo201901270014&billNo=billNo201901270014'
     * @param bindCardDto 交易参数
     * @return 通用json返回
     */
    @RequestMapping(value = "bindCard")
//    public ResponseEntity bindCard(@Valid BindCardDto bindCardDto, @SessionSubjectCode String subjectCode){
    public ResponseEntity bindCard(@Valid BindCardDto bindCardDto){
        ResponseEntity ret = new ResponseEntity(ResponseEntity.STATUS_OK);
        //初始化绑卡结果
        TransResult transResult = new TransResult();
        transResult.setBillNo(bindCardDto.getBillNo());
        transResult.setMerchantBillNo(bindCardDto.getMerchantBillNo());
        transResult.setTransStatus(0);
        transResult.setMsg("绑卡处理中");

        ret.setData(transResult);

        //调用通用绑卡接口
        transResult = bindCardService.bindCard(bindCardDto);

        if (null != transResult){
            return new ResponseEntity(ResponseEntity.STATUS_OK, transResult);
        }

        //如果通用绑卡接口异常，则默认绑卡处理中
        transResult.setTransStatus(0);
        transResult.setMsg("绑卡服务异常!请联系管理员核对状态!");
        return ret;
    }

    /**通用协议支付接口
     1.支付申请
     curl -i 'http://10.0.0.109:8089/protocolPay' -d 'step=1&name=成娓娓&phone=18321788695&cardNo=6217001210084599560&idCard=421302199202081224&amount=0.01&subjectCode=S000001&channelCode=CH3333333&merchantBillNo=merchantBillNo201901280004&billNo=billNo201901280004'
     2.支付确认
     curl -i 'http://10.0.0.109:8089/protocolPay' -d 'step=2&name=成娓娓&phone=18321788695&cardNo=6217001210084599560&idCard=421302199202081224&amount=0.01&smsCode=775463&subjectCode=S000001&channelCode=CH3333333&merchantBillNo=merchantBillNo201901280006&billNo=billNo201901280006'
     3.重发短信验证码
     curl -i 'http://10.0.0.109:8089/protocolPay' -d 'step=5&name=成娓娓&phone=18321788695&cardNo=6217001210084599560&idCard=421302199202081224&amount=0.01&subjectCode=S000001&channelCode=CH3333333&merchantBillNo=merchantBillNo201901280004&billNo=billNo201901280004'
     * @param transRequest 交易参数
     * @return 通用json返回
     */
    @RequestMapping(value = "protocolPay")
//    public ResponseEntity protocolPay(@Valid TransRequest transRequest ,@SessionSubjectCode String subjectCode){
    public ResponseEntity protocolPay(@Valid TransRequest transRequest){
        ResponseEntity ret = new ResponseEntity(ResponseEntity.STATUS_OK);
        transRequest.setServiceType(ServiceType.PROTOCOL_PAY.name());
        //初始化交易结果
        TransResult transResult = new TransResult();
        transResult.setMerchantBillNo(transRequest.getMerchantBillNo());
        transResult.setBillNo(transRequest.getBillNo());
        transResult.setAmount(transRequest.getAmount());
        transResult.setTransStatus(0);
        transResult.setMsg("协议支付处理中");
        transResult.setTransTime(System.currentTimeMillis());
        ret.setData(transResult);

        //调用通用协议支付接口
        transResult = protocolPayService.protocolPay(transRequest);

        if (null != transResult){
            return new ResponseEntity(ResponseEntity.STATUS_OK,transResult);
        }

        //如果通用协议支付接口异常，则默认协议支付处理中，防止重复交易
        transResult.setTransStatus(0);
        transResult.setMsg("协议支付服务异常!请联系管理员核对状态!");
        transResult.setTransTime(System.currentTimeMillis());
        return ret;
    }
}
